home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / info / internals.info-4.z / internals.info-4
Encoding:
GNU Info File  |  1998-05-21  |  47.0 KB  |  976 lines

  1. This is Info file ../../info/internals.info, produced by Makeinfo
  2. version 1.68 from the input file internals.texi.
  3.  
  4.    Copyright (C) 1992 - 1996 Ben Wing.  Copyright (C) 1996, 1997 Sun
  5. Microsystems.  Copyright (C) 1994, 1995 Free Software Foundation.
  6. Copyright (C) 1994, 1995 Board of Trustees, University of Illinois.
  7.  
  8.    Permission is granted to make and distribute verbatim copies of this
  9. manual provided the copyright notice and this permission notice are
  10. preserved on all copies.
  11.  
  12.    Permission is granted to copy and distribute modified versions of
  13. this manual under the conditions for verbatim copying, provided that the
  14. entire resulting derived work is distributed under the terms of a
  15. permission notice identical to this one.
  16.  
  17.    Permission is granted to copy and distribute translations of this
  18. manual into another language, under the above conditions for modified
  19. versions, except that this permission notice may be stated in a
  20. translation approved by the Foundation.
  21.  
  22.    Permission is granted to copy and distribute modified versions of
  23. this manual under the conditions for verbatim copying, provided also
  24. that the section entitled "GNU General Public License" is included
  25. exactly as in the original, and provided that the entire resulting
  26. derived work is distributed under the terms of a permission notice
  27. identical to this one.
  28.  
  29.    Permission is granted to copy and distribute translations of this
  30. manual into another language, under the above conditions for modified
  31. versions, except that the section entitled "GNU General Public License"
  32. may be included in a translation approved by the Free Software
  33. Foundation instead of in the original English.
  34.  
  35. 
  36. File: internals.info,  Node: Modules for Internationalization,  Prev: Modules for Interfacing with X Windows,  Up: A Summary of the Various XEmacs Modules
  37.  
  38. Modules for Internationalization
  39. ================================
  40.  
  41.         size  name
  42.      -------  ---------------------
  43.        42836  mule-canna.c
  44.        16737  mule-ccl.c
  45.        41080  mule-charset.c
  46.        30176  mule-charset.h
  47.       146844  mule-coding.c
  48.        16588  mule-coding.h
  49.         6996  mule-mcpath.c
  50.         2899  mule-mcpath.h
  51.        57158  mule-wnnfns.c
  52.         3351  mule.c
  53.  
  54.    These files implement the MULE (Asian-language) support.  Note that
  55. MULE actually provides a general interface for all sorts of languages,
  56. not just Asian languages (although they are generally the most
  57. complicated to support).  This code is still in beta.
  58.  
  59.    `mule-charset.*' and `mule-coding.*' provide the heart of the XEmacs
  60. MULE support.  `mule-charset.*' implements the "charset" Lisp object
  61. type, which encapsulates a character set (an ordered one- or
  62. two-dimensional set of characters, such as US ASCII or JISX0208 Japanese
  63. Kanji).
  64.  
  65.    `mule-coding.*' implements the "coding-system" Lisp object type,
  66. which encapsulates a method of converting between different encodings.
  67. An encoding is a representation of a stream of characters, possibly
  68. from multiple character sets, using a stream of bytes or words, and
  69. defines (e.g.) which escape sequences are used to specify particular
  70. character sets, how the indices for a character are converted into bytes
  71. (sometimes this involves setting the high bit; sometimes complicated
  72. rearranging of the values takes place, as in the Shift-JIS encoding),
  73. etc.
  74.  
  75.    `mule-ccl.c' provides the CCL (Code Conversion Language)
  76. interpreter.  CCL is similar in spirit to Lisp byte code and is used to
  77. implement converters for custom encodings.
  78.  
  79.    `mule-canna.c' and `mule-wnnfns.c' implement interfaces to external
  80. programs used to implement the Canna and WNN input methods,
  81. respectively.  This is currently in beta.
  82.  
  83.    `mule-mcpath.c' provides some functions to allow for pathnames
  84. containing extended characters.  This code is fragmentary, obsolete, and
  85. completely non-working.  Instead, PATHNAME-CODING-SYSTEM is used to
  86. specify conversions of names of files and directories.  The standard C
  87. I/O functions like `open()' are wrapped so that conversion occurs
  88. automatically.
  89.  
  90.    `mule.c' provides a few miscellaneous things that should probably be
  91. elsewhere.
  92.  
  93.         9400  intl.c
  94.  
  95.    This provides some miscellaneous internationalization code for
  96. implementing message translation and interfacing to the Ximp input
  97. method.  None of this code is currently working.
  98.  
  99.         1764  iso-wide.h
  100.  
  101.    This contains leftover code from an earlier implementation of
  102. Asian-language support, and is not currently used.
  103.  
  104. 
  105. File: internals.info,  Node: Allocation of Objects in XEmacs Lisp,  Next: Events and the Event Loop,  Prev: A Summary of the Various XEmacs Modules,  Up: Top
  106.  
  107. Allocation of Objects in XEmacs Lisp
  108. ************************************
  109.  
  110. * Menu:
  111.  
  112. * Introduction to Allocation::
  113. * Garbage Collection::
  114. * GCPROing::
  115. * Integers and Characters::
  116. * Allocation from Frob Blocks::
  117. * lrecords::
  118. * Low-level allocation::
  119. * Pure Space::
  120. * Cons::
  121. * Vector::
  122. * Bit Vector::
  123. * Symbol::
  124. * Marker::
  125. * String::
  126. * Bytecode::
  127.  
  128. 
  129. File: internals.info,  Node: Introduction to Allocation,  Next: Garbage Collection,  Up: Allocation of Objects in XEmacs Lisp
  130.  
  131. Introduction to Allocation
  132. ==========================
  133.  
  134.    Emacs Lisp, like all Lisps, has garbage collection.  This means that
  135. the programmer never has to explicitly free (destroy) an object; it
  136. happens automatically when the object becomes inaccessible.  Most
  137. experts agree that garbage collection is a necessity in a modern,
  138. high-level language.  Its omission from C stems from the fact that C was
  139. originally designed to be a nice abstract layer on top of assembly
  140. language, for writing kernels and basic system utilities rather than
  141. large applications.
  142.  
  143.    Lisp objects can be created by any of a number of Lisp primitives.
  144. Most object types have one or a small number of basic primitives for
  145. creating objects.  For conses, the basic primitive is `cons'; for
  146. vectors, the primitives are `make-vector' and `vector'; for symbols,
  147. the primitives are `make-symbol' and `intern'; etc.  Some Lisp objects,
  148. especially those that are primarily used internally, have no
  149. corresponding Lisp primitives.  Every Lisp object, though, has at least
  150. one C primitive for creating it.
  151.  
  152.    Recall from section (VII) that a Lisp object, as stored in a 32-bit
  153. or 64-bit word, has a mark bit, a few tag bits, and a "value" that
  154. occupies the remainder of the bits.  We can separate the different Lisp
  155. object types into four broad categories:
  156.  
  157.    * (a) Those for whom the value directly represents the contents of
  158.      the Lisp object.  Only two types are in this category: integers and
  159.      characters.  No special allocation or garbage collection is
  160.      necessary for such objects.  Lisp objects of these types do not
  161.      need to be `GCPRO'ed.
  162.  
  163.    In the remaining three categories, the value is a pointer to a
  164. structure.
  165.  
  166.    * (b) Those for whom the tag directly specifies the type.  Recall
  167.      that there are only three tag bits; this means that at most five
  168.      types can be specified this way.  The most commonly-used types are
  169.      stored in this format; this includes conses, strings, vectors, and
  170.      sometimes symbols.  With the exception of vectors, objects in this
  171.      category are allocated in "frob blocks", i.e. large blocks of
  172.      memory that are subdivided into individual objects.  This saves a
  173.      lot on malloc overhead, since there are typically quite a lot of
  174.      these objects around, and the objects are small.  (A cons, for
  175.      example, occupies 8 bytes on 32-bit machines - 4 bytes for each of
  176.      the two objects it contains.) Vectors are individually
  177.      `malloc()'ed since they are of variable size.  (It would be
  178.      possible, and desirable, to allocate vectors of certain small
  179.      sizes out of frob blocks, but it isn't currently done.) Strings
  180.      are handled specially: Each string is allocated in two parts, a
  181.      fixed size structure containing a length and a data pointer, and
  182.      the actual data of the string.  The former structure is allocated
  183.      in frob blocks as usual, and the latter data is stored in "string
  184.      chars blocks" and is relocated during garbage collection to
  185.      eliminate holes.
  186.  
  187.    In the remaining two categories, the type is stored in the object
  188. itself.  The tag for all such objects is the generic "lrecord"
  189. (Lisp_Record) tag.  The first four bytes (or eight, for 64-bit machines)
  190. of the object's structure are a pointer to a structure that describes
  191. the object's type, which includes method pointers and a pointer to a
  192. string naming the type.  Note that it's possible to save some space by
  193. using a one- or two-byte tag, rather than a four- or eight-byte pointer
  194. to store the type, but it's not clear it's worth making the change.
  195.  
  196.    * (c) Those lrecords that are allocated in frob blocks (see above).
  197.      This includes the objects that are most common and relatively
  198.      small, and includes floats, bytecodes, symbols (when not in
  199.      category (b)), extents, events, and markers.  With the cleanup of
  200.      frob blocks done in 19.12, it's not terribly hard to add more
  201.      objects to this category, but it's a bit trickier than adding an
  202.      object type to type (d) (esp. if the object needs a finalization
  203.      method), and is not likely to save much space unless the object is
  204.      small and there are many of them. (In fact, if there are very few
  205.      of them, it might actually waste space.)
  206.  
  207.    * (d) Those lrecords that are individually `malloc()'ed.  These are
  208.      called "lcrecords".  All other types are in this category.  Adding
  209.      a new type to this category is comparatively easy, and all types
  210.      added since 19.8 (when the current allocation scheme was devised,
  211.      by Richard Mlynarik), with the exception of the character type,
  212.      have been in this category.
  213.  
  214.    Note that bit vectors are a bit of a special case.  They are simple
  215. lrecords as in category (c), but are individually `malloc()'ed like
  216. vectors.  You can basically view them as exactly like vectors except
  217. that their type is stored in lrecord fashion rather than in
  218. directly-tagged fashion.
  219.  
  220.    Note that FSF Emacs redesigned their object system in 19.29 to follow
  221. a similar scheme.  However, given RMS's expressed dislike for data
  222. abstraction, the FSF scheme is not nearly as clean or as easy to
  223. extend. (FSF calls items of type (c) `Lisp_Misc' and items of type (d)
  224. `Lisp_Vectorlike', with separate tags for each, although
  225. `Lisp_Vectorlike' is also used for vectors.)
  226.  
  227. 
  228. File: internals.info,  Node: Garbage Collection,  Next: GCPROing,  Prev: Introduction to Allocation,  Up: Allocation of Objects in XEmacs Lisp
  229.  
  230. Garbage Collection
  231. ==================
  232.  
  233.    Garbage collection is simple in theory but tricky to implement.
  234. Emacs Lisp uses the oldest garbage collection method, called "mark and
  235. sweep".  Garbage collection begins by starting with all accessible
  236. locations (i.e. all variables and other slots where Lisp objects might
  237. occur) and recursively traversing all objects accessible from those
  238. slots, marking each one that is found.  We then go through all of
  239. memory and free each object that is not marked, and unmarking each
  240. object that is marked.  Note that "all of memory" means all currently
  241. allocated objects.  Traversing all these objects means traversing all
  242. frob blocks, all vectors (which are chained in one big list), and all
  243. lcrecords (which are likewise chained).
  244.  
  245.    Note that, when an object is marked, the mark has to occur inside of
  246. the object's structure, rather than in the 32-bit `Lisp_Object' holding
  247. the object's pointer; i.e. you can't just set the pointer's mark bit.
  248. This is because there may be many pointers to the same object.  This
  249. means that the method of marking an object can differ depending on the
  250. type.  The different marking methods are approximately as follows:
  251.  
  252.   1. For conses, the mark bit of the car is set.
  253.  
  254.   2. For strings, the mark bit of the string's plist is set.
  255.  
  256.   3. For symbols when not lrecords, the mark bit of the symbol's plist
  257.      is set.
  258.  
  259.   4. For vectors, the length is negated after adding 1.
  260.  
  261.   5. For lrecords, the pointer to the structure describing the type is
  262.      changed (see below).
  263.  
  264.   6. Integers and characters do not need to be marked, since no
  265.      allocation occurs for them.
  266.  
  267.    The details of this are in the `mark_object()' function.
  268.  
  269.    Note that any code that operates during garbage collection has to be
  270. especially careful because of the fact that some objects may be marked
  271. and as such may not look like they normally do.  In particular:
  272.  
  273.      Some object pointers may have their mark bit set.  This will make
  274.      `FOOBARP()' predicates fail.  Use `GC_FOOBARP()' to deal with this.
  275.  
  276.    * Even if you clear the mark bit, `FOOBARP()' will still fail for
  277.      lrecords because the implementation pointer has been changed (see
  278.      below).  `GC_FOOBARP()' will correctly deal with this.
  279.  
  280.    * Vectors have their size field munged, so anything that looks at
  281.      this field will fail.
  282.  
  283.    * Note that `XFOOBAR()' macros *will* work correctly on object
  284.      pointers with their mark bit set, because the logical shift
  285.      operations that remove the tag also remove the mark bit.
  286.  
  287.    Finally, note that garbage collection can be invoked explicitly by
  288. calling `garbage-collect' but is also called automatically by `eval',
  289. once a certain amount of memory has been allocated since the last
  290. garbage collection (according to `gc-cons-threshold').
  291.  
  292. 
  293. File: internals.info,  Node: GCPROing,  Next: Integers and Characters,  Prev: Garbage Collection,  Up: Allocation of Objects in XEmacs Lisp
  294.  
  295. `GCPRO'ing
  296. ==========
  297.  
  298.    `GCPRO'ing is one of the ugliest and trickiest parts of Emacs
  299. internals.  The basic idea is that whenever garbage collection occurs,
  300. all in-use objects must be reachable somehow or other from one of the
  301. roots of accessibility.  The roots of accessibility are:
  302.  
  303.   1. All objects that have been `staticpro()'d.  This is used for any
  304.      global C variables that hold Lisp objects.  A call to
  305.      `staticpro()' happens implicitly as a result of any symbols
  306.      declared with `defsymbol()' and any variables declared with
  307.      `DEFVAR_FOO()'.  You need to explicitly call `staticpro()' (in the
  308.      `vars_of_foo()' method of a module) for other global C variables
  309.      holding Lisp objects. (This typically includes internal lists and
  310.      such things.)
  311.  
  312.      Note that `obarray' is one of the `staticpro()'d things.
  313.      Therefore, all functions and variables get marked through this.
  314.  
  315.   2. Any shadowed bindings that are sitting on the specpdl stack.
  316.  
  317.   3. Any objects sitting in currently active (Lisp) stack frames,
  318.      catches, and condition cases.
  319.  
  320.   4. A couple of special-case places where active objects are located.
  321.  
  322.   5. Anything currently marked with `GCPRO'.
  323.  
  324.    Marking with `GCPRO' is necessary because some C functions (quite a
  325. lot, in fact), allocate objects during their operation.  Quite
  326. frequently, there will be no other pointer to the object while the
  327. function is running, and if a garbage collection occurs and the object
  328. needs to be referenced again, bad things will happen.  The solution is
  329. to mark those objects with `GCPRO'.  Unfortunately this is easy to
  330. forget, and there is basically no way around this problem.  Here are
  331. some rules, though:
  332.  
  333.   1. For every `GCPRON', there have to be declarations of `struct gcpro
  334.      gcpro1, gcpro2', etc.
  335.  
  336.   2. You *must* `UNGCPRO' anything that's `GCPRO'ed, and you *must not*
  337.      `UNGCPRO' if you haven't `GCPRO'ed.  Getting either of these wrong
  338.      will lead to crashes, often in completely random places unrelated
  339.      to where the problem lies.
  340.  
  341.   3. The way this actually works is that all currently active `GCPRO's
  342.      are chained through the `struct gcpro' local variables, with the
  343.      variable `gcprolist' pointing to the head of the list and the nth
  344.      local `gcpro' variable pointing to the first `gcpro' variable in
  345.      the next enclosing stack frame.  Each `GCPRO'ed thing is an
  346.      lvalue, and the `struct gcpro' local variable contains a pointer to
  347.      this lvalue.  This is why things will mess up badly if you don't
  348.      pair up the `GCPRO's and `UNGCPRO's - you will end up with
  349.      `gcprolist's containing pointers to `struct gcpro's or local
  350.      `Lisp_Object' variables in no-longer-active stack frames.
  351.  
  352.   4. It is actually possible for a single `struct gcpro' to protect a
  353.      contiguous array of any number of values, rather than just a
  354.      single lvalue.  To effect this, call `GCPRON' as usual on the
  355.      first object in the array and then set `gcpron.nvars'.
  356.  
  357.   5. *Strings are relocated.*  What this means in practice is that the
  358.      pointer obtained using `XSTRING_DATA()' is liable to change at any
  359.      time, and you should never keep it around past any function call,
  360.      or pass it as an argument to any function that might cause a
  361.      garbage collection.  This is why a number of functions accept
  362.      either a "non-relocatable" `char *' pointer or a relocatable Lisp
  363.      string, and only access the Lisp string's data at the very last
  364.      minute.  In some cases, you may end up having to `alloca()' some
  365.      space and copy the string's data into it.
  366.  
  367.   6. By convention, if you have to nest `GCPRO''s, use `NGCPRON' (along
  368.      with `struct gcpro ngcpro1, ngcpro2', etc.), `NNGCPRON', etc.
  369.      This avoids compiler warnings about shadowed locals.
  370.  
  371.   7. It is *always* better to err on the side of extra `GCPRO's rather
  372.      than too few.  The extra cycles spent on this are almost never
  373.      going to make a whit of difference in the speed of anything.
  374.  
  375.   8. The general rule to follow is that caller, not callee, `GCPRO's.
  376.      That is, you should not have to explicitly `GCPRO' any Lisp objects
  377.      that are passed in as parameters, but if you create any Lisp
  378.      objects (remember, this happens in all sorts of circumstances,
  379.      e.g. with `Fcons()', etc.), you are responsible for `GCPRO'ing the
  380.      objects unless you are *absolutely sure* that there's no
  381.      possibility that a garbage-collection can occur while you need to
  382.      use the object.  Even then, consider `GCPRO'ing.
  383.  
  384.   9. A garbage collection can occur whenever anything calls `Feval', or
  385.      whenever a QUIT can occur where execution can continue past this.
  386.      (Remember, this is almost anywhere.)
  387.  
  388.  10. If you have the *least smidgeon of doubt* about whether you need
  389.      to `GCPRO', you should `GCPRO'.
  390.  
  391.  11. Beware of `GCPRO'ing something that is uninitialized.  If you have
  392.      any shade of doubt about this, initialize all your variables to
  393.      `Qnil'.
  394.  
  395.  12. Be careful of traps, like calling `Fcons()' in the argument to
  396.      another function.  By the "caller protects" law, you should be
  397.      `GCPRO'ing the newly-created cons, but you aren't.  A certain
  398.      number of functions that are commonly called on freshly created
  399.      stuff (e.g. `nconc2()', `Fsignal()'), break the "caller protects"
  400.      law and go ahead and `GCPRO' their arguments so as to simplify
  401.      things, but make sure and check if it's OK whenever doing
  402.      something like this.
  403.  
  404.  13. Once again, remember to `GCPRO'!  Bugs resulting from insufficient
  405.      `GCPRO'ing are intermittent and extremely difficult to track down,
  406.      often showing up in crashes inside of `garbage-collect' or in
  407.      weirdly corrupted objects or even in incorrect values in a totally
  408.      different section of code.
  409.  
  410.    Given the extremely error-prone nature of the `GCPRO' scheme, and
  411. the difficulties in tracking down, it should be considered a deficiency
  412. in the XEmacs code.  A solution to this problem would involve
  413. implementing so-called "conservative" garbage collection for the C
  414. stack.  That involves looking through all of stack memory and treating
  415. anything that looks like a reference to an object as a reference.  This
  416. will result in a few objects not getting collected when they should, but
  417. it obviates the need for `GCPRO'ing, and allows garbage collection to
  418. happen at any point at all, such as during object allocation.
  419.  
  420. 
  421. File: internals.info,  Node: Integers and Characters,  Next: Allocation from Frob Blocks,  Prev: GCPROing,  Up: Allocation of Objects in XEmacs Lisp
  422.  
  423. Integers and Characters
  424. =======================
  425.  
  426.    Integer and character Lisp objects are created from integers using
  427. the macros `XSETINT()' and `XSETCHAR()' or the equivalent functions
  428. `make_int()' and `make_char()'. (These are actually macros on most
  429. systems.)  These functions basically just do some moving of bits
  430. around, since the integral value of the object is stored directly in
  431. the `Lisp_Object'.
  432.  
  433.    `XSETINT()' and the like will truncate values given to them that are
  434. too big; i.e. you won't get the value you expected but the tag bits
  435. will at least be correct.
  436.  
  437. 
  438. File: internals.info,  Node: Allocation from Frob Blocks,  Next: lrecords,  Prev: Integers and Characters,  Up: Allocation of Objects in XEmacs Lisp
  439.  
  440. Allocation from Frob Blocks
  441. ===========================
  442.  
  443.    The uninitialized memory required by a `Lisp_Object' of a particular
  444. type is allocated using `ALLOCATE_FIXED_TYPE()'.  This only occurs
  445. inside of the lowest-level object-creating functions in `alloc.c':
  446. `Fcons()', `make_float()', `Fmake_byte_code()', `Fmake_symbol()',
  447. `allocate_extent()', `allocate_event()', `Fmake_marker()', and
  448. `make_uninit_string()'.  The idea is that, for each type, there are a
  449. number of frob blocks (each 2K in size); each frob block is divided up
  450. into object-sized chunks.  Each frob block will have some of these
  451. chunks that are currently assigned to objects, and perhaps some that are
  452. free. (If a frob block has nothing but free chunks, it is freed at the
  453. end of the garbage collection cycle.)  The free chunks are stored in a
  454. free list, which is chained by storing a pointer in the first four bytes
  455. of the chunk. (Except for the free chunks at the end of the last frob
  456. block, which are handled using an index which points past the end of the
  457. last-allocated chunk in the last frob block.)  `ALLOCATE_FIXED_TYPE()'
  458. first tries to retrieve a chunk from the free list; if that fails, it
  459. calls `ALLOCATE_FIXED_TYPE_FROM_BLOCK()', which looks at the end of the
  460. last frob block for space, and creates a new frob block if there is
  461. none. (There are actually two versions of these macros, one of which is
  462. more defensive but less efficient and is used for error-checking.)
  463.  
  464. 
  465. File: internals.info,  Node: lrecords,  Next: Low-level allocation,  Prev: Allocation from Frob Blocks,  Up: Allocation of Objects in XEmacs Lisp
  466.  
  467. lrecords
  468. ========
  469.  
  470.    [see `lrecord.h']
  471.  
  472.    All lrecords have at the beginning of their structure a `struct
  473. lrecord_header'.  This just contains a pointer to a `struct
  474. lrecord_implementation', which is a structure containing method pointers
  475. and such.  There is one of these for each type, and it is a global,
  476. constant, statically-declared structure that is declared in the
  477. `DEFINE_LRECORD_IMPLEMENTATION()' macro. (This macro actually declares
  478. an array of two `struct lrecord_implementation' structures.  The first
  479. one contains all the standard method pointers, and is used in all
  480. normal circumstances.  During garbage collection, however, the lrecord
  481. is "marked" by bumping its implementation pointer by one, so that it
  482. points to the second structure in the array.  This structure contains a
  483. special indication in it that it's a "marked-object" structure: the
  484. finalize method is the special function `this_marks_a_marked_record()',
  485. and all other methods are null pointers.  At the end of garbage
  486. collection, all lrecords will either be reclaimed or unmarked by
  487. decrementing their implementation pointers, so this second structure
  488. pointer will never remain past garbage collection.
  489.  
  490.    Simple lrecords (of type (c) above) just have a `struct
  491. lrecord_header' at their beginning.  lcrecords, however, actually have a
  492. `struct lcrecord_header'.  This, in turn, has a `struct lrecord_header'
  493. at its beginning, so sanity is preserved; but it also has a pointer
  494. used to chain all lcrecords together, and a special ID field used to
  495. distinguish one lcrecord from another. (This field is used only for
  496. debugging and could be removed, but the space gain is not significant.)
  497.  
  498.    Simple lrecords are created using `ALLOCATE_FIXED_TYPE()', just like
  499. for other frob blocks.  The only change is that the implementation
  500. pointer must be initialized correctly. (The implementation structure for
  501. an lrecord, or rather the pointer to it, is named `lrecord_float',
  502. `lrecord_extent', `lrecord_buffer', etc.)
  503.  
  504.    lcrecords are created using `alloc_lcrecord()'.  This takes a size
  505. to allocate and an implementation pointer. (The size needs to be passed
  506. because some lcrecords, such as window configurations, are of variable
  507. size.) This basically just `malloc()'s the storage, initializes the
  508. `struct lcrecord_header', and chains the lcrecord onto the head of the
  509. list of all lcrecords, which is stored in the variable `all_lcrecords'.
  510. The calls to `alloc_lcrecord()' generally occur in the lowest-level
  511. allocation function for each lrecord type.
  512.  
  513.    Whenever you create an lrecord, you need to call either
  514. `DEFINE_LRECORD_IMPLEMENTATION()' or
  515. `DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION()'.  This needs to be specified
  516. in a C file, at the top level.  What this actually does is define and
  517. initialize the implementation structure for the lrecord. (And possibly
  518. declares a function `error_check_foo()' that implements the `XFOO()'
  519. macro when error-checking is enabled.)  The arguments to the macros are
  520. the actual type name (this is used to construct the C variable name of
  521. the lrecord implementation structure and related structures using the
  522. `##' macro concatenation operator), a string that names the type on the
  523. Lisp level (this may not be the same as the C type name; typically, the
  524. C type name has underscores, while the Lisp string has dashes), various
  525. method pointers, and the name of the C structure that contains the
  526. object.  The methods are used to encapsulate type-specific information
  527. about the object, such as how to print it or mark it for garbage
  528. collection, so that it's easy to add new object types without having to
  529. add a specific case for each new type in a bunch of different places.
  530.  
  531.    The difference between `DEFINE_LRECORD_IMPLEMENTATION()' and
  532. `DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION()' is that the former is used
  533. for fixed-size object types and the latter is for variable-size object
  534. types.  Most object types are fixed-size; some complex types, however
  535. (e.g. window configurations), are variable-size.  Variable-size object
  536. types have an extra method, which is called to determine the actual
  537. size of a particular object of that type.  (Currently this is only used
  538. for keeping allocation statistics.)
  539.  
  540.    For the purpose of keeping allocation statistics, the allocation
  541. engine keeps a list of all the different types that exist.  Note that,
  542. since `DEFINE_LRECORD_IMPLEMENTATION()' is a macro that is specified at
  543. top-level, there is no way for it to add to the list of all existing
  544. types.  What happens instead is that each implementation structure
  545. contains in it a dynamically assigned number that is particular to that
  546. type. (Or rather, it contains a pointer to another structure that
  547. contains this number.  This evasiveness is done so that the
  548. implementation structure can be declared const.) In the sweep stage of
  549. garbage collection, each lrecord is examined to see if its
  550. implementation structure has its dynamically-assigned number set.  If
  551. not, it must be a new type, and it is added to the list of known types
  552. and a new number assigned.  The number is used to index into an array
  553. holding the number of objects of each type and the total memory
  554. allocated for objects of that type.  The statistics in this array are
  555. also computed during the sweep stage.  These statistics are returned by
  556. the call to `garbage-collect' and are printed out at the end of the
  557. loadup phase.
  558.  
  559.    Note that for every type defined with a `DEFINE_LRECORD_*()' macro,
  560. there needs to be a `DECLARE_LRECORD_IMPLEMENTATION()' somewhere in a
  561. `.h' file, and this `.h' file needs to be included by `inline.c'.
  562.  
  563.    Furthermore, there should generally be a set of `XFOOBAR()',
  564. `FOOBARP()', etc. macros in a `.h' (or occasionally `.c') file.  To
  565. create one of these, copy an existing model and modify as necessary.
  566.  
  567.    The various methods in the lrecord implementation structure are:
  568.  
  569.   1. A "mark" method.  This is called during the marking stage and
  570.      passed a function pointer (usually the `mark_object()' function),
  571.      which is used to mark an object.  All Lisp objects that are
  572.      contained within the object need to be marked by applying this
  573.      function to them.  The mark method should also return a Lisp
  574.      object, which should be either nil or an object to mark. (This can
  575.      be used in lieu of calling `mark_object()' on the object, to
  576.      reduce the recursion depth, and consequently should be the most
  577.      heavily nested sub-object, such as a long list.)
  578.  
  579.      *Note*: When the mark method is called, garbage collection is in
  580.      progress, and special precautions need to be taken when accessing
  581.      objects; see section (B) above.
  582.  
  583.      If your mark method does not need to do anything, it can be `NULL'.
  584.  
  585.   2. A "print" method.  This is called to create a printed
  586.      representation of the object, whenever `princ', `prin1', or the
  587.      like is called.  It is passed the object, a stream to which the
  588.      output is to be directed, and an `escapeflag' which indicates
  589.      whether the object's printed representation should be "escaped" so
  590.      that it is readable. (This corresponds to the difference between
  591.      `princ' and `prin1'.) Basically, "escaped" means that strings will
  592.      have quotes around them and confusing characters in the strings
  593.      such as quotes, backslashes, and newlines will be backslashed; and
  594.      that special care will be taken to make symbols print in a
  595.      readable fashion (e.g. symbols that look like numbers will be
  596.      backslashed).  Other readable objects should perhaps pass
  597.      `escapeflag' on when sub-objects are printed, so that readability
  598.      is preserved when necessary (or if not, always pass in a 1 for
  599.      `escapeflag').  Non-readable objects should in general ignore
  600.      `escapeflag', except that some use it as an indication that more
  601.      verbose output should be given.
  602.  
  603.      Sub-objects are printed using `print_internal()', which takes
  604.      exactly the same arguments as are passed to the print method.
  605.  
  606.      Literal C strings should be printed using `write_c_string()', or
  607.      `write_string_1()' for non-null-terminated strings.
  608.  
  609.      Functions that do not have a readable representation should check
  610.      the `print_readably' flag and signal an error if it is set.
  611.  
  612.      If you specify NULL for the print method, the
  613.      `default_object_printer()' will be used.
  614.  
  615.   3. A "finalize" method.  This is called at the beginning of the sweep
  616.      stage on lcrecords that are about to be freed, and should be used
  617.      to perform any extra object cleanup.  This typically involves
  618.      freeing any extra `malloc()'ed memory associated with the object,
  619.      releasing any operating-system and window-system resources
  620.      associated with the object (e.g. pixmaps, fonts), etc.
  621.  
  622.      The finalize method can be NULL if nothing needs to be done.
  623.  
  624.      WARNING #1: The finalize method is also called at the end of the
  625.      dump phase; this time with the for_disksave parameter set to
  626.      non-zero.  The object is *not* about to disappear, so you have to
  627.      make sure to *not* free any extra `malloc()'ed memory if you're
  628.      going to need it later.  (Also, signal an error if there are any
  629.      operating-system and window-system resources here, because they
  630.      can't be dumped.)
  631.  
  632.      Finalize methods should, as a rule, set to zero any pointers after
  633.      they've been freed, and check to make sure pointers are not zero
  634.      before freeing.  Although I'm pretty sure that finalize methods
  635.      are not called twice on the same object (except for the
  636.      `for_disksave' proviso), we've gotten nastily burned in some cases
  637.      by not doing this.
  638.  
  639.      WARNING #2: The finalize method is *only* called for lcrecords,
  640.      *not* for simply lrecords.  If you need a finalize method for
  641.      simple lrecords, you have to stick it in the
  642.      `ADDITIONAL_FREE_foo()' macro in `alloc.c'.
  643.  
  644.      WARNING #3: Things are in an *extremely* bizarre state when
  645.      `ADDITIONAL_FREE_foo()' is called, so you have to be incredibly
  646.      careful when writing one of these functions.  See the comment in
  647.      `gc_sweep()'.  If you ever have to add one of these, consider
  648.      using an lcrecord or dealing with the problem in a different
  649.      fashion.
  650.  
  651.   4. An "equal" method.  This compares the two objects for similarity,
  652.      when `equal' is called.  It should compare the contents of the
  653.      objects in some reasonable fashion.  It is passed the two objects
  654.      and a "depth" value, which is used to catch circular objects.  To
  655.      compare sub-Lisp-objects, call `internal_equal()' and bump the
  656.      depth value by one.  If this value gets too high, a
  657.      `circular-object' error will be signaled.
  658.  
  659.      If this is NULL, objects are `equal' only when they are `eq', i.e.
  660.      identical.
  661.  
  662.   5. A "hash" method.  This is used to hash objects when they are to be
  663.      compared with `equal'.  The rule here is that if two objects are
  664.      `equal', they *must* hash to the same value; i.e. your hash
  665.      function should use some subset of the sub-fields of the object
  666.      that are compared in the "equal" method.  If you specify this
  667.      method as `NULL', the object's pointer will be used as the hash,
  668.      which will *fail* if the object has an `equal' method, so don't do
  669.      this.
  670.  
  671.      To hash a sub-Lisp-object, call `internal_hash()'.  Bump the depth
  672.      by one, just like in the "equal" method.
  673.  
  674.      To convert a Lisp object directly into a hash value (using its
  675.      pointer), use `LISP_HASH()'.  This is what happens when the hash
  676.      method is NULL.
  677.  
  678.      To hash two or more values together into a single value, use
  679.      `HASH2()', `HASH3()', `HASH4()', etc.
  680.  
  681.   6. "getprop", "putprop", "remprop", and "plist" methods.  These are
  682.      used for object types that have properties.  I don't feel like
  683.      documenting them here.  If you create one of these objects, you
  684.      have to use different macros to define them, i.e.
  685.      `DEFINE_LRECORD_IMPLEMENTATION_WITH_PROPS()' or
  686.      `DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION_WITH_PROPS()'.
  687.  
  688.   7. A "size_in_bytes" method, when the object is of variable-size.
  689.      (i.e. declared with a `_SEQUENCE_IMPLEMENTATION' macro.)  This
  690.      should simply return the object's size in bytes, exactly as you
  691.      might expect.  For an example, see the methods for window
  692.      configurations and opaques.
  693.  
  694. 
  695. File: internals.info,  Node: Low-level allocation,  Next: Pure Space,  Prev: lrecords,  Up: Allocation of Objects in XEmacs Lisp
  696.  
  697. Low-level allocation
  698. ====================
  699.  
  700.    Memory that you want to allocate directly should be allocated using
  701. `xmalloc()' rather than `malloc()'.  This implements error-checking on
  702. the return value, and once upon a time did some more vital stuff (i.e.
  703. `BLOCK_INPUT', which is no longer necessary).  Free using `xfree()',
  704. and realloc using `xrealloc()'.  Note that `xmalloc()' will do a
  705. non-local exit if the memory can't be allocated. (Many functions,
  706. however, do not expect this, and thus XEmacs will likely crash if this
  707. happens.  *This is a bug.*  If you can, you should strive to make your
  708. function handle this OK.  However, it's difficult in the general
  709. circumstance, perhaps requiring extra unwind-protects and such.)
  710.  
  711.    Note that XEmacs provides two separate replacements for the standard
  712. `malloc()' library function.  These are called "old GNU malloc"
  713. (`malloc.c') and "new GNU malloc" (`gmalloc.c'), respectively.  New GNU
  714. malloc is better in pretty much every way than old GNU malloc, and
  715. should be used if possible.  (It used to be that on some systems, the
  716. old one worked but the new one didn't.  I think this was due
  717. specifically to a bug in SunOS, which the new one now works around; so
  718. I don't think the old one ever has to be used any more.) The primary
  719. difference between both of these mallocs and the standard system malloc
  720. is that they are much faster, at the expense of increased space.  The
  721. basic idea is that memory is allocated in fixed chunks of powers of
  722. two.  This allows for basically constant malloc time, since the various
  723. chunks can just be kept on a number of free lists. (The standard system
  724. malloc typically allocates arbitrary-sized chunks and has to spend some
  725. time, sometimes a significant amount of time, walking the heap looking
  726. for a free block to use and cleaning things up.)  The new GNU malloc
  727. improves on things by allocating large objects in chunks of 4096 bytes
  728. rather than in ever larger powers of two, which results in ever larger
  729. wastage.  There is a slight speed loss here, but it's of doubtful
  730. significance.
  731.  
  732.    NOTE: Apparently there is a third-generation GNU malloc that is
  733. significantly better than the new GNU malloc, and should probably be
  734. included in XEmacs.
  735.  
  736.    There is also the relocating allocator, `ralloc.c'.  This actually
  737. moves blocks of memory around so that the `sbrk()' pointer shrunk and
  738. virtual memory released back to the system.  On some systems, this is a
  739. big win.  On all systems, it causes a noticeable (and sometimes huge)
  740. speed penalty, so I turn it off by default.  `ralloc.c' only works with
  741. the new GNU malloc in `gmalloc.c'.  There are also two versions of
  742. `ralloc.c', one that uses `mmap()' rather than block copies to move
  743. data around.  This purports to be faster, although that depends on the
  744. amount of data that would have had to be block copied and the
  745. system-call overhead for `mmap()'.  I don't know exactly how this
  746. works, except that the relocating-allocation routines are pretty much
  747. used only for the memory allocated for a buffer, which is the biggest
  748. consumer of space, esp. of space that may get freed later.
  749.  
  750.    Note that the GNU mallocs have some "memory warning" facilities.
  751. XEmacs taps into them and issues a warning through the standard warning
  752. system, when memory gets to 75%, 85%, and 95% full.  (On some systems,
  753. the memory warnings are not functional.)
  754.  
  755.    Allocated memory that is going to be used to make a Lisp object is
  756. created using `allocate_lisp_storage()'.  This calls `xmalloc()' but
  757. also verifies that the pointer to the memory can fit into a Lisp word
  758. (remember that some bits are taken away for a type tag and a mark bit).
  759. If not, an error is issued through `memory_full()'.
  760. `allocate_lisp_storage()' is called by `alloc_lcrecord()',
  761. `ALLOCATE_FIXED_TYPE()', and the vector and bit-vector creation
  762. routines.  These routines also call `INCREMENT_CONS_COUNTER()' at the
  763. appropriate times; this keeps statistics on how much memory is
  764. allocated, so that garbage-collection can be invoked when the threshold
  765. is reached.
  766.  
  767. 
  768. File: internals.info,  Node: Pure Space,  Next: Cons,  Prev: Low-level allocation,  Up: Allocation of Objects in XEmacs Lisp
  769.  
  770. Pure Space
  771. ==========
  772.  
  773.    Not yet documented.
  774.  
  775. 
  776. File: internals.info,  Node: Cons,  Next: Vector,  Prev: Pure Space,  Up: Allocation of Objects in XEmacs Lisp
  777.  
  778. Cons
  779. ====
  780.  
  781.    Conses are allocated in standard frob blocks.  The only thing to
  782. note is that conses can be explicitly freed using `free_cons()' and
  783. associated functions `free_list()' and `free_alist()'.  This
  784. immediately puts the conses onto the cons free list, and decrements the
  785. statistics on memory allocation appropriately.  This is used to good
  786. effect by some extremely commonly-used code, to avoid generating extra
  787. objects and thereby triggering GC sooner.  However, you have to be
  788. *extremely* careful when doing this.  If you mess this up, you will get
  789. BADLY BURNED, and it has happened before.
  790.  
  791. 
  792. File: internals.info,  Node: Vector,  Next: Bit Vector,  Prev: Cons,  Up: Allocation of Objects in XEmacs Lisp
  793.  
  794. Vector
  795. ======
  796.  
  797.    As mentioned above, each vector is `malloc()'ed individually, and
  798. all are threaded through the variable `all_vectors'.  Vectors are
  799. marked strangely during garbage collection, by kludging the size field.
  800. Note that the `struct Lisp_Vector' is declared with its `contents'
  801. field being a *stretchy* array of one element.  It is actually
  802. `malloc()'ed with the right size, however, and access to any element
  803. through the `contents' array works fine.
  804.  
  805. 
  806. File: internals.info,  Node: Bit Vector,  Next: Symbol,  Prev: Vector,  Up: Allocation of Objects in XEmacs Lisp
  807.  
  808. Bit Vector
  809. ==========
  810.  
  811.    Bit vectors work exactly like vectors, except for more complicated
  812. code to access an individual bit, and except for the fact that bit
  813. vectors are lrecords while vectors are not. (The only difference here is
  814. that there's an lrecord implementation pointer at the beginning and the
  815. tag field in bit vector Lisp words is "lrecord" rather than "vector".)
  816.  
  817. 
  818. File: internals.info,  Node: Symbol,  Next: Marker,  Prev: Bit Vector,  Up: Allocation of Objects in XEmacs Lisp
  819.  
  820. Symbol
  821. ======
  822.  
  823.    Symbols are also allocated in frob blocks.  Note that the code
  824. exists for symbols to be either lrecords (category (c) above) or simple
  825. types (category (b) above), and are lrecords by default (I think),
  826. although there is no good reason for this.
  827.  
  828.    Note that symbols in the awful horrible obarray structure are
  829. chained through their `next' field.
  830.  
  831.    Remember that `intern' looks up a symbol in an obarray, creating one
  832. if necessary.
  833.  
  834. 
  835. File: internals.info,  Node: Marker,  Next: String,  Prev: Symbol,  Up: Allocation of Objects in XEmacs Lisp
  836.  
  837. Marker
  838. ======
  839.  
  840.    Markers are allocated in frob blocks, as usual.  They are kept in a
  841. buffer unordered, but in a doubly-linked list so that they can easily
  842. be removed. (Formerly this was a singly-linked list, but in some cases
  843. garbage collection took an extraordinarily long time due to the O(N^2)
  844. time required to remove lots of markers from a buffer.) Markers are
  845. removed from a buffer in the finalize stage, in
  846. `ADDITIONAL_FREE_marker()'.
  847.  
  848. 
  849. File: internals.info,  Node: String,  Next: Bytecode,  Prev: Marker,  Up: Allocation of Objects in XEmacs Lisp
  850.  
  851. String
  852. ======
  853.  
  854.    As mentioned above, strings are a special case.  A string is
  855. logically two parts, a fixed-size object (containing the length,
  856. property list, and a pointer to the actual data), and the actual data
  857. in the string.  The fixed-size object is a `struct Lisp_String' and is
  858. allocated in frob blocks, as usual.  The actual data is stored in
  859. special "string-chars blocks", which are 8K blocks of memory.
  860. Currently-allocated strings are simply laid end to end in these
  861. string-chars blocks, with a pointer back to the `struct Lisp_String'
  862. stored before each string in the string-chars block.  When a new string
  863. needs to be allocated, the remaining space at the end of the last
  864. string-chars block is used if there's enough, and a new string-chars
  865. block is created otherwise.
  866.  
  867.    There are never any holes in the string-chars blocks due to the
  868. string compaction and relocation that happens at the end of garbage
  869. collection.  During the sweep stage of garbage collection, when objects
  870. are reclaimed, the garbage collector goes through all string-chars
  871. blocks, looking for unused strings.  Each chunk of string data is
  872. preceded by a pointer to the corresponding `struct Lisp_String', which
  873. indicates both whether the string is used and how big the string is,
  874. i.e. how to get to the next chunk of string data.  Holes are compressed
  875. by block-copying the next string into the empty space and relocating the
  876. pointer stored in the corresponding `struct Lisp_String'.  *This means
  877. you have to be careful with strings in your code.* See the section
  878. above on `GCPRO'ing.
  879.  
  880.    Note that there is one situation not handled: a string that is too
  881. big to fit into a string-chars block.  Such strings, called "big
  882. strings", are all `malloc()'ed as their own block. (#### Although it
  883. would make more sense for the threshold for big strings to be somewhat
  884. lower, e.g. 1/2 or 1/4 the size of a string-chars block.  It seems that
  885. this was indeed the case formerly - indeed, the threshold was set at
  886. 1/8 - but Mly forgot about this when rewriting things for 19.8.)
  887.  
  888.    Note also that the string data in string-chars blocks is padded as
  889. necessary so that proper alignment constraints on the `struct
  890. Lisp_String' back pointers are maintained.
  891.  
  892.    Finally, strings can be resized.  This happens in Mule when a
  893. character is substituted with a different-length character, or during
  894. modeline frobbing. (You could also export this to Lisp, but it's not
  895. done so currently.) Resizing a string is a potentially tricky process.
  896. If the change is small enough that the padding can absorb it, nothing
  897. other than a simple memory move needs to be done.  Keep in mind,
  898. however, that the string can't shrink too much because the offset to the
  899. next string in the string-chars block is computed by looking at the
  900. length and rounding to the nearest multiple of four or eight.  If the
  901. string would shrink or expand beyond the correct padding, new string
  902. data needs to be allocated at the end of the last string-chars block and
  903. the data moved appropriately.  This leaves some dead string data, which
  904. is marked by putting a special marker of 0xFFFFFFFF in the `struct
  905. Lisp_String' pointer before the data (there's no real `struct
  906. Lisp_String' to point to and relocate), and storing the size of the dead
  907. string data (which would normally be obtained from the now-non-existent
  908. `struct Lisp_String') at the beginning of the dead string data gap.
  909. The string compactor recognizes this special 0xFFFFFFFF marker and
  910. handles it correctly.
  911.  
  912. 
  913. File: internals.info,  Node: Bytecode,  Prev: String,  Up: Allocation of Objects in XEmacs Lisp
  914.  
  915. Bytecode
  916. ========
  917.  
  918.    Not yet documented.
  919.  
  920. 
  921. File: internals.info,  Node: Events and the Event Loop,  Next: Evaluation; Stack Frames; Bindings,  Prev: Allocation of Objects in XEmacs Lisp,  Up: Top
  922.  
  923. Events and the Event Loop
  924. *************************
  925.  
  926. * Menu:
  927.  
  928. * Introduction to Events::
  929. * Main Loop::
  930. * Specifics of the Event Gathering Mechanism::
  931. * Specifics About the Emacs Event::
  932. * The Event Stream Callback Routines::
  933. * Other Event Loop Functions::
  934. * Converting Events::
  935. * Dispatching Events; The Command Builder::
  936.  
  937. 
  938. File: internals.info,  Node: Introduction to Events,  Next: Main Loop,  Up: Events and the Event Loop
  939.  
  940. Introduction to Events
  941. ======================
  942.  
  943.    An event is an object that encapsulates information about an
  944. interesting occurrence in the operating system.  Events are generated
  945. either by user action, direct (e.g. typing on the keyboard or moving
  946. the mouse) or indirect (moving another window, thereby generating an
  947. expose event on an Emacs frame), or as a result of some other typically
  948. asynchronous action happening, such as output from a subprocess being
  949. ready or a timer expiring.  Events come into the system in an
  950. asynchronous fashion (typically through a callback being called) and
  951. are converted into a synchronous event queue (first-in, first-out) in a
  952. process that we will call "collection".
  953.  
  954.    Note that each application has its own event queue. (It is
  955. immaterial whether the collection process directly puts the events in
  956. the proper application's queue, or puts them into a single system
  957. queue, which is later split up.)
  958.  
  959.    The most basic level of event collection is done by the operating
  960. system or window system.  Typically, XEmacs does its own event
  961. collection as well.  Often there are multiple layers of collection in
  962. XEmacs, with events from various sources being collected into a queue,
  963. which is then combined with other sources to go into another queue
  964. (i.e. a second level of collection), with perhaps another level on top
  965. of this, etc.
  966.  
  967.    XEmacs has its own types of events (called "Emacs events"), which
  968. provides an abstract layer on top of the system-dependent nature of the
  969. most basic events that are received.  Part of the complex nature of the
  970. XEmacs event collection process involves converting from the
  971. operating-system events into the proper Emacs events - there may not be
  972. a one-to-one correspondence.
  973.  
  974.    Emacs events are documented in `events.h'; I'll discuss them later.
  975.  
  976.